package com.liangtian.mdm.dm.jsp.util;

import com.liangtian.mdm.base.utils.ReadSystemParam;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.jsp.dao.JspActionDao;
import com.liangtian.mdm.dm.listener.CacheUtil;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/12/4.
 */
@Component
public class WFparamUtil {
    @Autowired
    private JspActionDao jspActionDao;
    @Autowired
    private CacheUtil cacheUtil;


    public Map<String, Object> formatWfParam(Map<String, Object> map) {
        Map<String, Object> vars = new HashMap<>();
        String type = (String) map.get("type");
        if (!StringUtils.isEmpty(type) && "submit".equals(type)) {
            SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
            vars.put("usrid", user.getOrganizationId());
        }
        String columns = cacheUtil.getColumns(map.get("tableName").toString().split("_")[1]);
        map.put("columns", columns.substring(0, columns.length() - 1));
        Map<String, Object> data = jspActionDao.queryDetail(map);
        try {
            String sysFileType = ReadSystemParam.getValue((String) map.get("tableName"));
            if (!StringUtils.isEmpty(sysFileType)) {
                String[] rule = sysFileType.split(";");
                for (String str :
                        rule) {
                    String value = (String) (ObjectUtils.isEmpty(data.get(str.split(":")[1])) ? "0" : data.get(str.split(":")[1]));
                    vars.put(str.split(":")[0], value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vars;
    }

}
